﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Data;
using IBatisNet.DataMapper;

namespace uIBTv.Service
{
    public class UngVienService
    {
        #region private fields
        private QueryIDs _QueryIDs;
        private DataSet _DataSetUngVien;
        private SqlMapper sqlMapper;
        private Model.UngVienCollection _UngVienCollection;
        #endregion

        #region Properties
        public QueryIDs QueryIDs
        {
            get { return _QueryIDs; }
            set { _QueryIDs = value; }
        }

        public DataSet DataSetUngVien
        {
            get { return _DataSetUngVien; }
        }
        #endregion

        #region Methods
        public UngVienService()
        {
            TcpChannel chan = new TcpChannel(8084);

            // Lets the remoting system use the socket
            ChannelServices.RegisterChannel(chan, false);

            // Associate the class HelloServer with the URI "SayHello"
            RemotingConfiguration.RegisterWellKnownServiceType(
                typeof(UngVienService),  //type
                "UngVienService",  //uri
                WellKnownObjectMode.SingleCall); //mode
        }

        public UngVienService(Model.UngVienCollection UngVienCollection)
        {
            this._UngVienCollection = UngVienCollection;
            this._DataSetUngVien = UngVienCollection.ToDataSet();
            this._DataSetUngVien.AcceptChanges();

            _QueryIDs = new QueryIDs();
            this._QueryIDs.Select = "GetMany";
            this._QueryIDs.Insert = "InsertOneUngVien";
            this._QueryIDs.Update = "UpdateOneUngVien";
            this._QueryIDs.Delete = "DeleteOneUngVien";

        }

        private Domain.UngVien GetUngVien(DataRow row)
        {
            Domain.UngVien UngVien = new uIBTv.Domain.UngVien();

            UngVien.Id_Ungvien = new Guid();
            UngVien.Ten_UngVien = "" + row["Ten_UngVien"];
            
            return UngVien;
        }

        public DataSet GetUngVienCollection(Model.UngVienCollection UngVienCollection)
        {
            this._DataSetUngVien = UngVienCollection.ToDataSet();
            return this._DataSetUngVien;
        }

        public bool UpdateUngVienCollection()
        {
            try
            {
                DataSet dsI = _DataSetUngVien.GetChanges(DataRowState.Added);
                DataSet dsU = _DataSetUngVien.GetChanges(DataRowState.Modified);
                DataSet dsD = _DataSetUngVien.GetChanges(DataRowState.Deleted);
                
                sqlMapper = Service.Mapper.Instance();

                sqlMapper.BeginTransaction();

                if (dsI != null)
                foreach (DataRow r in dsI.Tables[0].Rows)
                    sqlMapper.Insert(this._QueryIDs.Insert, this.GetUngVien(r));

                if (dsU != null)
                foreach (DataRow r in dsU.Tables[0].Rows)
                    sqlMapper.Update(this._QueryIDs.Update, this.GetUngVien(r));

                if (dsD != null)
                {
                    dsD.RejectChanges();
                    foreach (DataRow r in dsD.Tables[0].Rows)
                        sqlMapper.Update(this._QueryIDs.Delete, this.GetUngVien(r));
                }

               sqlMapper.CommitTransaction();

               _DataSetUngVien.AcceptChanges();
                return true;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.ToString());
                sqlMapper.RollBackTransaction();
                return false;
            }
        }

        public bool UpdateUngVienCollection(DataSet UngVienCollection)
        {
            this._DataSetUngVien = UngVienCollection;
            return UpdateUngVienCollection();
        }

        #endregion
    }
}
